package com.sdry.web.controller.lz;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.sdry.model.lz.LzQueryCriteria;
import com.sdry.model.lz.Samplekeep;
import com.sdry.service.lz.SamplekeepService;
import com.sdry.utils.ResponseUtil;
import com.sdry.utils.StringUtil;

/**
 * @ClassName SamplekeepController
 * @Description 留样管理
 * @Author lz
 * @Date 2018年12月24日 14:00:55
 * @Version 1.0
 */
@Controller
@RequestMapping("/samplekeep")
public class SamplekeepController {

	public static final Integer AFFECT_ROW = 0;
	@Resource
	SamplekeepService samplekeepService;
	
    @RequestMapping("/addSamplekeep")
    public void addSamplekeep(HttpServletResponse response, Samplekeep samplekeep) throws Exception {
    	if (samplekeep != null) {
			if(StringUtil.isEmpty(samplekeep.getSamplekeepTime())){
				samplekeep.setSamplekeepTime(null);
			}
			if(StringUtil.isEmpty(samplekeep.getInvalidTime())){
				samplekeep.setInvalidTime(null);
			}
		}
        Long affectRow = samplekeepService.addSamplekeep(samplekeep);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 单个删除和批量删除
     * @param response
     * @param idArr id数组
     * @throws Exception
     */
    @RequestMapping("/deleteSamplekeepById")
    public void deleteSamplekeepById(HttpServletResponse response, @RequestParam("idArr[]") String[] idArr) throws Exception {
        for (int i = 0; i < idArr.length; i++) {
            Long affectRow = samplekeepService.deleteSamplekeepById(Long.parseLong(idArr[i]));
            if (affectRow > AFFECT_ROW) {
                ResponseUtil.write(response, affectRow);
            } else {
                ResponseUtil.write(response, AFFECT_ROW);
            }
        }
    }

    /**
     * 根据ID修改设备信息
     * @param response
     * @param samplekeep 选中行的设备
     * @throws Exception
     */
    @RequestMapping("/editSamplekeepById")
    public void editSamplekeepById(HttpServletResponse response, Samplekeep samplekeep) throws Exception {
    	if (samplekeep != null) {
			if(StringUtil.isEmpty(samplekeep.getSamplekeepTime())){
				samplekeep.setSamplekeepTime(null);
			}
			if(StringUtil.isEmpty(samplekeep.getInvalidTime())){
				samplekeep.setInvalidTime(null);
			}
		}
        Long affectRow = samplekeepService.editSamplekeepById(samplekeep);
        if (affectRow > AFFECT_ROW) {
            ResponseUtil.write(response, affectRow);
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }

    /**
     * 分页查询留样管理
     * @param criteria
     */
    @RequestMapping("/querySamplekeep")
    public void querySamplekeep(HttpServletResponse response, LzQueryCriteria criteria){
        String keyword01 = criteria.getKeyword01();
        String keyword02 = criteria.getKeyword02();
        if(keyword01 == null){
            keyword01 = "";
        }
        if(keyword02 == null){
            keyword02 = "";
        }
        criteria.setKeyword01(keyword01);
        criteria.setKeyword02(keyword02);
        List<Samplekeep> samplekeepList = samplekeepService.querySamplekeep(criteria);
        //int count = samplekeepService.countSamplekeep(criteria);
        int count = samplekeepList.size();
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = JSONArray.fromObject(samplekeepList);
        jsonObject.put("code", AFFECT_ROW);
        jsonObject.put("data", jsonArray);
        jsonObject.put("count", count);
        try {
            ResponseUtil.write(response, jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 根据Id查询留样实体
     * @param response
     * @param id
     * @throws Exception
     */
    @RequestMapping("/queryFilesURLById")
    public void queryFilesURLById(HttpServletResponse response, String id) throws Exception {
    	Samplekeep samplekeep = samplekeepService.queryFilesURLById(id);
        if (samplekeep != null) {
            ResponseUtil.write(response, samplekeep.getUploadAttachment());
        } else {
            ResponseUtil.write(response, AFFECT_ROW);
        }
    }
}
